# Michael Depuy
# depuym@onid.orst.edu
# CS344-400
# Homework 6

import socket
import os
import subprocess
import sys
import signal

HOST = socket.gethostname()
PORT = 9879
PORT_STR = "9879"

NUM_CONNS = 5

PERF_NUMS_ARRAY = []

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

def handler(signum,frame):
    global sock
    print "Signal caught. Terminating..."
    sock.close()
    sys.exit(0)
# Establish handlers
signal.signal(signal.SIGINT,handler)
signal.signal(signal.SIGHUP,handler)
signal.signal(signal.SIGQUIT,handler)
# Create socket

# Bind socket
sock.bind((HOST,PORT))

# Listen
sock.listen(NUM_CONNS)

# Do stuff
while True:
    (client,address) = sock.accept()
    client_type = client.recv(8)
    if client_type == '1': # Enter into compute functions
        print "Compute client connected."
        client_ops = client.recv(32)
        print "Amount of ops client can handle",client_ops
    else: # Enter into report functions
        print "Report connected. Sending info and aborting."
        sock.close()
        sys.exit(0)
    client.close()
# Close on INTR, QUIT, HANGUP signals
